import { queryPage, queryInfoById, create, lock, update, queryOrterInfoById } from './service';
import { queryDictList } from '@/services/dict';
import { queryList } from '@/pages/data/build/service';
import { routerRedux } from 'dva/router';
import { DICT } from './enum';

function setList(list, index) {
  list[index].status = list[index].status === 'lock' ? 'normal' : 'lock';
  return list;
}

export default {
  namespace: 'device_alarm_breakdown',

  state: {
    list: { data: [] },
    details: [],
    cond: {},
    dict: {}
  },

  effects: {
    *initCond(_, { put }) {
      yield put({ type: 'setCond', payload: {} });
      yield put({ type: 'queryPage' })
    },
    *updateCond({ payload }, { put }) {
      yield put({ type: 'setCond', payload });
      yield put({ type: 'queryPage' })
    },
    *queryPage({ payload = { pageNum: 1, pageSize: 10, rawType: '3'  } }, { call, put, select }) {
      const cond = yield select(state => state.device_alarm_breakdown.cond);
      const params = { ...payload, ...cond };
      const { list = [], total } = yield call(queryPage, params);
      yield put({ type: 'queryDict', payload: {keyList: DICT} });
      yield put({ type: 'setPage', payload: { list, total } })
    },
    *queryDict({ payload }, { call, put }) {
      const dict = yield call(queryDictList, payload);
      yield put({ type: 'setDict', payload: dict })
    },
    *queryDictList({ payload }, { call, put }) {  // 下拉列表查
      const dictList = yield call(queryList, payload);
      yield put({ type: 'setDictList', payload: dictList })
    },
    *queryInfoById({ payload }, { call, put }) {
      const response = yield call(queryInfoById, payload);
      yield put({ type: 'setInfo', payload: response })
    },
    *queryOrterInfoById({ payload }, { call, put }) {
      yield put({ type: 'queryDictList' });
      const response = yield call(queryOrterInfoById, payload);
      yield put({ type: 'setOrterInfo', payload: response })
    },
    *queryEditInfoById({ payload }, { call, put }) {
      const response = yield call(queryInfoById, payload.id);
      const items = {};

      payload.keys.forEach(item => {
        items[item] = response[item]
      });
      yield put({ type: 'setEditInfo', payload: items })
    },
    *create({ payload }, { call, put }) {
      yield call(create, payload);
      yield put(routerRedux.push('/device_fire_host/person'));
    },
    *update({ payload }, { call, put }) {
      yield call(update, payload);
      yield put(routerRedux.push('/device_fire_host/person'));
    },
    *lock({ payload }, { all, call, put }) {
      yield all([
        call(lock, payload.id),
        put({ type: 'setOneLock', payload: payload.ind })
      ]);
    }
  },

  reducers: {
    setCond(state, action) {
      return {
        ...state,
        cond: action.payload
      }
    },
    setPage(state, action) {
      return {
        ...state,
        list: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setDict(state, action) {
      return {
        ...state,
        dict: action.payload
      }
    },
    setDictList(state, action) {
      return {
        ...state,
        dictList: action.payload
      }
    },
    setInfo(state, action) {
      return {
        ...state,
        details: action.payload
      }
    },
    setOrterInfo(state, action) {
      return {
        ...state,
        orterDetail: action.payload
      }
    },
    setEditInfo(state, action) {
      return {
        ...state,
        editDetails: action.payload
      }
    },
    setOneLock(state, action) {
      return {
        ...state,
        list: {
          ...state.list,
          data: setList(state.list.data, action.payload)
        }
      }
    }
  }
}
